#!/bin/bash

# MongoDB用户管理工具备份脚本

echo "=== 开始备份 ==="

# 创建备份目录
BACKUP_DIR="backups/$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"

# 备份MySQL数据库
echo "📦 备份MySQL数据库..."
if command -v mysqldump &> /dev/null; then
    mysqldump -h ${MYSQL_HOST:-localhost} \
              -u ${MYSQL_USER:-root} \
              -p${MYSQL_PASSWORD:-root} \
              ${MYSQL_DATABASE:-mongod_sys} > "$BACKUP_DIR/mysql_backup.sql"
    echo "✅ MySQL备份完成: $BACKUP_DIR/mysql_backup.sql"
else
    echo "⚠️  mysqldump未找到，跳过MySQL备份"
fi

# 备份配置文件
echo "📦 备份配置文件..."
cp config.json "$BACKUP_DIR/" 2>/dev/null || echo "⚠️  config.json不存在"
cp .env "$BACKUP_DIR/" 2>/dev/null || echo "⚠️  .env不存在"

echo "✅ 备份完成: $BACKUP_DIR"